package com.example.controller;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;

import com.example.listenanddriveproject.R;

public class TreatmentFragment extends Fragment {
	
	private Context context;
	private Switch angleSwitch;
	private Switch affichageSwitch;
	private Button button;
	private boolean boolFishEye;
	private boolean boolVideoDisplay;
	private View rootView;
	
	public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		
		rootView = inflater.inflate(R.layout.fragment_video_treatment, container, false);
		
		context = this.getActivity();
		
		angleSwitch = (Switch) rootView.findViewById(R.id.switchAngle);
	    angleSwitch.setChecked(MainActivity.settings.getVideoSettings().isFisheye());
	    angleSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				boolFishEye=isChecked;
			}       

		});
	    
	    affichageSwitch = (Switch) rootView.findViewById(R.id.switchAffichage);
	    affichageSwitch.setChecked(MainActivity.settings.getVideoSettings().isVideoDisplay());
	    affichageSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				boolVideoDisplay=isChecked;
			}       

		});
	    
	    if(MainActivity.settings.getVideoSettings().isCompleteTreatment()){
	    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonComplet);
	    	button.setChecked(true);
	    }else if(MainActivity.settings.getVideoSettings().isPartialTreatment()){
	    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonPartiel);
	    	button.setChecked(true);
	    }else if(MainActivity.settings.getVideoSettings().isNoTreatment()){
	    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonSans);
	    	button.setChecked(true);
	    }
	    
		button = (Button) rootView.findViewById(R.id.apply1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup);
            	
            	// get selected radio button from radioGroup
        		int selectedId = radioGroup.getCheckedRadioButtonId();

        		// find the radiobutton by returned id
        	    RadioButton radioButton = (RadioButton) rootView.findViewById(selectedId);
        	    
    		    if(radioButton != null){
	    		    if(String.valueOf(radioButton.getText()).equalsIgnoreCase("Complet")){
	    		    	MainActivity.settings.getVideoSettings().setCompleteTreatment(true);
	    		    	MainActivity.settings.getVideoSettings().setPartialTreatment(false);
	    		    	MainActivity.settings.getVideoSettings().setNoTreatment(false);
	    		    }else if(String.valueOf(radioButton.getText()).equalsIgnoreCase("Partiel")){
	    		    	MainActivity.settings.getVideoSettings().setCompleteTreatment(false);
	    		    	MainActivity.settings.getVideoSettings().setPartialTreatment(true);
	    		    	MainActivity.settings.getVideoSettings().setNoTreatment(false);
	    		    }else{
	    		    	MainActivity.settings.getVideoSettings().setCompleteTreatment(false);
	    		    	MainActivity.settings.getVideoSettings().setPartialTreatment(false);
	    		    	MainActivity.settings.getVideoSettings().setNoTreatment(true);
	    		    }
    		    }
    		    MainActivity.settings.getVideoSettings().setFisheye(boolFishEye);
    		    MainActivity.settings.getVideoSettings().setVideoDisplay(boolVideoDisplay);
            	MainActivity.settings.saveArray(context);
            }
        });
        
		return rootView;
	}
	
	 @Override
	    public void setUserVisibleHint(boolean isVisibleToUser) {
	        super.setUserVisibleHint(isVisibleToUser);

	        // Make sure that we are currently visible
	        if (this.isVisible()) {
	            if (isVisibleToUser) {
	            	if(MainActivity.settings.getVideoSettings().isRefresh()){
	        			angleSwitch.setChecked(MainActivity.settings.getVideoSettings().isFisheye());
	        			affichageSwitch.setChecked(MainActivity.settings.getVideoSettings().isVideoDisplay());
	        			
	        			if(MainActivity.settings.getVideoSettings().isCompleteTreatment()){
	        		    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonComplet);
	        		    	button.setChecked(true);
	        		    }else if(MainActivity.settings.getVideoSettings().isPartialTreatment()){
	        		    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonPartiel);
	        		    	button.setChecked(true);
	        		    }else if(MainActivity.settings.getVideoSettings().isNoTreatment()){
	        		    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonSans);
	        		    	button.setChecked(true);
	        		    }
	        			
	        			MainActivity.settings.getVideoSettings().setRefresh(false);
	        		}
	            }
	        }
	    }
	
	/**
	 * refresh the UI when the reset is called
	 */
	@Override
	public void onResume(){
		super.onResume();
		
		if(MainActivity.settings.getVideoSettings().isRefresh()){
			angleSwitch.setChecked(MainActivity.settings.getVideoSettings().isFisheye());
			affichageSwitch.setChecked(MainActivity.settings.getVideoSettings().isVideoDisplay());
			
			if(MainActivity.settings.getVideoSettings().isCompleteTreatment()){
		    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonComplet);
		    	button.setChecked(true);
		    }else if(MainActivity.settings.getVideoSettings().isPartialTreatment()){
		    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonPartiel);
		    	button.setChecked(true);
		    }else if(MainActivity.settings.getVideoSettings().isNoTreatment()){
		    	RadioButton button = (RadioButton) rootView.findViewById(R.id.radioButtonSans);
		    	button.setChecked(true);
		    }
			
			MainActivity.settings.getVideoSettings().setRefresh(false);
		}
		
	}

}
